抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Guess the number

1. 题目

  • I’m thinking of a number. All you have to do is guess it.
  • 题目代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// SPDX-License-Identifier: MIT
pragma solidity ^0.4.21;

contract GuessTheNumberChallenge {
uint8 answer = 42;

function GuessTheNumberChallenge() public payable {
require(msg.value == 1 ether);
}

function isComplete() public view returns (bool) {
return address(this).balance == 0;
}

function guess(uint8 n) public payable {
require(msg.value == 1 ether);

if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}

2. 分析

  • 2.1 这题是猜数字,但是他的数字是写死的,通过 ethers读取。
  • 2.2 但是部署和 猜数字都需要支付 1ether ,即两个,如果猜对了他就会返回之前支付的ether

3. 解题

  • 3.1 部署,msg.value = 1 ether

  • ![image-20240412144722655](Guess the number/image-20240412144722655.png)

  • 3.2 调用 guss() 函数

  • ![image-20240412144732902](Guess the number/image-20240412144732902.png)

  • 3.3 查看合约余额

  • ![image-20240412144740840](Guess the number/image-20240412144740840.png)

评论



政策 · 统计 | 本站使用 Volantis 主题设计